//
//  RootViewController.m
//  Prism
//
//  Created by Bas du Pre on 16-05-11.
//  Copyright UU 2011. All rights reserved.
//

//
// RootViewController + iAd
// If you want to support iAd, use this class as the controller of your iAd
//

#include "global.h"
#if PRISM_OS_IOS

#import "cocos2d.h"

#import "RootViewController.h"
#import "ios_settings.h"
#include "Director.h"

@implementation RootViewController

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
	// Custom initialization
	}
	return self;
 }
 */

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {
 }
 */

/*
 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad {
	[super viewDidLoad];
 }
 */

#ifndef PRISM_AUTOROTATION
#error PRISM_AUTOROTATION has not been defined
#endif

- (BOOL)canRotateToOrientation:(UIInterfaceOrientation)interfaceOrientation {
    NSArray *orients = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
	
    for (NSString *orient in orients) {
        switch (interfaceOrientation) {
            case UIInterfaceOrientationLandscapeLeft: {
                if ([orient isEqualToString:@"UIInterfaceOrientationLandscapeLeft"])
                    return YES;
            } break;
            case UIInterfaceOrientationLandscapeRight: {
                if ([orient isEqualToString:@"UIInterfaceOrientationLandscapeRight"])
                    return YES;
            } break;
            case UIInterfaceOrientationPortrait: {
                if ([orient isEqualToString:@"UIInterfaceOrientationPortrait"])
                    return YES;
            } break;
            case UIInterfaceOrientationPortraitUpsideDown: {
                if ([orient isEqualToString:@"UIInterfaceOrientationPortraitUpsideDown"])
                    return YES;
            } break;
                
            default:
                return NO;
                break;
        }
    }
	return NO;
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	
	//
	// There are 2 ways to support auto-rotation:
	//  - The OpenGL / cocos2d way
	//     - Faster, but doesn't rotate the UIKit objects
	//  - The ViewController way
	//    - A bit slower, but the UiKit objects are placed in the right place
	//
	
#if PRISM_AUTOROTATION==PRISM_AUTOROTATION_NONE
	//
	// EAGLView won't be autorotated.
	// Since this method should return YES in at least 1 orientation, 
	// we return YES only in the Portrait orientation
	//
	return ( interfaceOrientation == UIInterfaceOrientationPortrait );
	
#elif PRISM_AUTOROTATION==PRISM_AUTOROTATION_DIRECTOR
    
    if ([self canRotateToOrientation:interfaceOrientation]) {
        if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
            [[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeRight];
        } else if( interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            [[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeLeft];
        } else if( interfaceOrientation == UIInterfaceOrientationPortrait) {
            [[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationPortrait];
        } else {
            [[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationPortraitUpsideDown];
        }
        //return YES;
    }
    
    if (Prism::Director::isCreated()) {
        CGSize native_winSize = [[CCDirector sharedDirector] winSize];
        Prism::theDirector()->setWinSize(Prism::Size(native_winSize.width, native_winSize.height));
    }
        
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
	
	//return NO;
	
#elif PRISM_AUTOROTATION == PRISM_AUTOROTATION_UIVIEWCONTROLLER
	//
	// EAGLView will be rotated by the UIViewController
	//
	// Sample: Autorotate only in landscpe mode
	//
	// return YES for the supported orientations
    
    return [self canRotateToOrientation:interfaceOrientation];    
    
#else
#error Unknown value in PRISM_AUTOROTATION
	
#endif // GAME_AUTOROTATION
	
	
	// Shold not happen
	return NO;
}

//
// This callback only will be called when GAME_AUTOROTATION == kGameAutorotationUIViewController
//
#if PRISM_AUTOROTATION == PRISM_AUTOROTATION_UIVIEWCONTROLLER
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
	//
	// Assuming that the main window has the size of the screen
	// BUG: This won't work if the EAGLView is not fullscreen
	///
	CGRect screenRect = [[UIScreen mainScreen] bounds];
	CGRect rect = CGRectZero;

	
	if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)		
		rect = screenRect;
	
	else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
		rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );
	
	CCDirector *director = [CCDirector sharedDirector];
	EAGLView *glView = [director openGLView];
	float contentScaleFactor = [director contentScaleFactor];
	
	if( contentScaleFactor != 1 ) {
		rect.size.width *= contentScaleFactor;
		rect.size.height *= contentScaleFactor;
	}
	glView.frame = rect;
    
    Prism::theDirector()->setWinSize(Prism::Size(rect.size.width, rect.size.height));
}
#endif // GAME_AUTOROTATION == kGameAutorotationUIViewController


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

#endif
